﻿namespace Site2AppWP8.Config
{
    using System.Runtime.Serialization;

    [DataContract]
    public class Rule
    {
        public Rule()
        {
        }

        public string PatternRegex
        {
            get
            {
                return this.Pattern;
            }
        }

        [DataMember(Name = "pattern")]
        public string Pattern { get; set; }

        [DataMember(Name = "action")]
        public string Action { get; set; }

        public string RedirectOnMatchRegex
        {
            get
            {
                return this.RedirectOnMatch;
            }
        }

        [DataMember(Name = "redirectOnMatch")]
        public string RedirectOnMatch { get; set; }

        [DataMember(Name = "redirectUrl")]
        public string RedirectUrl { get; set; }

        private string CreateRegexFrom(string pattern)
        {
            var regexBody = System.Text.RegularExpressions.Regex.Replace(pattern ?? string.Empty, @"/([.?*+^$[\]\\(){}|-])/g", "\\$1");
            regexBody = System.Text.RegularExpressions.Regex.Replace(regexBody, @"/\\\?/g", ".?");
            regexBody = System.Text.RegularExpressions.Regex.Replace(regexBody, @"/\\\*/g", ".*?");
            regexBody = "^" + regexBody;
            regexBody += "$";

            return regexBody;
        }
    }
}
